package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.BasicStandardRestrictedPart;
import cc.comac.mom.dao.entity.BasicStandardRestrictedPartDao;
import cc.comac.mom.dao.entity.QualityQarCheckInvoiceDao;
import cc.comac.mom.entity.BasicStandardRestrictedPartEntity;
import cc.comac.mom.entity.QarRestrictedPartEntity;
import cc.comac.mom.entity.QarRestrictedPartItemEntity;
import cc.comac.mom.entity.QualityQarCheckInvoiceEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.MomConstants;
@Service
public class BasicStandardRestrictedPartDataHandler extends ServiceImpl<BasicStandardRestrictedPartDao, BasicStandardRestrictedPartEntity>{

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	@Autowired
	private BasicStandardRestrictedPartDao basicStandardRestrictedPartDao;
	@Autowired
	private QarRestrictedPartDataHandler qarRestrictedPartDataHandler;
	
	@Autowired
	private QualityQarCheckInvoiceDao qualityQarCheckInvoiceDao;
	@Autowired
	private QarRestrictedPartItemDataHandler qarRestrictedPartItemDataHandler;
	public void batchInsertData (List<BasicStandardRestrictedPart> list) {
		if (!CollectionUtils.isEmpty(list)) {
			List<BasicStandardRestrictedPartEntity> entitylist = list.stream().map(BasicStandardRestrictedPart::getInnerData).filter(item -> item != null).collect(Collectors.toList());
			if (!CollectionUtils.isEmpty(entitylist)) {
				entitylist.forEach(item -> {
					item.setUuid(UUID.randomUUID().toString());
					baseEntityDataUtil.setBaseEntityData(item);
					});
				this.saveBatch(entitylist, entitylist.size());
			}
		}
	}

	/**
	 * 通过传入的数据在母本升版时更新限寿件对应数据
	 * **/ 
	public void checkUpdateRestrictedPartData (List<BasicStandardRestrictedPart> list,long basicStandarId) {
			if (!CollectionUtils.isEmpty(list)) {
//				list.stream().map(BasicStandardRestrictedPart::getInnerdata)
			}
		QueryWrapper<BasicStandardRestrictedPartEntity> queryWrapper = new QueryWrapper<BasicStandardRestrictedPartEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("basic_standard_id", basicStandarId);
		// 查询原母本对应的限售件集合
		List<BasicStandardRestrictedPartEntity> selectList = basicStandardRestrictedPartDao.selectList(queryWrapper);
		if (!CollectionUtils.isEmpty(selectList)) {
			List<Long> collect = selectList.stream().map(item -> item.getId()).collect(Collectors.toList());
			this.removeByIds(collect);
		}
		if (!CollectionUtils.isEmpty(list)) {
			List<BasicStandardRestrictedPartEntity> entityList = list.stream().map(BasicStandardRestrictedPart::getInnerData).filter(item -> item != null).collect(Collectors.toList());
			List<BasicStandardRestrictedPartEntity> addList = entityList.stream().filter(item -> {
				item.setBasicStandardId(basicStandarId);
				item.setUuid(UUID.randomUUID().toString());
				baseEntityDataUtil.setBaseEntityData(item);
				return item.getId()==0;
			}).collect(Collectors.toList());
			List<BasicStandardRestrictedPartEntity> updateList = entityList.stream().filter(item -> {
				item.setBasicStandardId(basicStandarId);
				baseEntityDataUtil.updateBaseEntityData(item);
				return item.getId()!=0;
			}).collect(Collectors.toList());
			if (addList.size() > 0) {
				this.saveBatch(addList, addList.size());
			}
			if (updateList.size() >0) {
				updateList.forEach(item -> item.setBasicStandardId(basicStandarId));
				this.saveBatch(updateList, updateList.size());
			}
		}
	}
	
	public List<QarRestrictedPartItemEntity> clonePartItemEntityList(List<QarRestrictedPartItemEntity> list) {
		List<QarRestrictedPartItemEntity> entityList = new ArrayList<>();
		if (list.size() > 0) {
			for (QarRestrictedPartItemEntity item : list) {
				QarRestrictedPartItemEntity entity = new QarRestrictedPartItemEntity();
				entity.setBaseTime(item.getBaseTime());
				entity.setDueProcessing(item.getDueProcessing());
				entity.setId(item.getId());
				entity.setLifeLimitedMethod(item.getLifeLimitedMethod());
				entity.setLifeLimitedTimeType(item.getLifeLimitedTimeType());
				entity.setLifeLimitedTimeValue(item.getLifeLimitedTimeValue());
				entity.setParentId(item.getParentId());
				entity.setQarNo(item.getQarNo());
				entity.setStorageTime(item.getStorageTime());
				entity.setUuid(item.getUuid());
				entityList.add(entity);
			}
		}
		return entityList;
	}
	/**
	 * 母本升版提交时将对应的数据更新到符合条件的qar中
	 * **/ 
	public void updateQarCheckDataByRestrictedPart (List<BasicStandardRestrictedPart> list,long basicStandarId) {
		QueryWrapper<QualityQarCheckInvoiceEntity> queryWrapper = new QueryWrapper<QualityQarCheckInvoiceEntity>();
		queryWrapper.eq("qar_basic_standard_id", basicStandarId);
		queryWrapper.eq("row_status", 1);
		// 只查询已发布，检验中，委托中三个状态数据
		queryWrapper.in("current_status", Arrays.asList(MomConstants.QUALITY_QAR_CURRENT_STATUS_ISSUE,
				MomConstants.QUALITY_QAR_CURRENT_STATUS_CHECKING, MomConstants.QUALITY_QAR_CURRENT_STATUS_ENTRUST));
		List<QualityQarCheckInvoiceEntity> selectOne = qualityQarCheckInvoiceDao.selectList(queryWrapper);
		List<String> qarList = selectOne.stream().map(item -> item.getQarNo()).filter(item -> item != null).collect(Collectors.toList());
		List<QarRestrictedPartItemEntity> addList = new ArrayList<QarRestrictedPartItemEntity>();
		List<QarRestrictedPartItemEntity> deleteList = new ArrayList<QarRestrictedPartItemEntity>();
		List<QarRestrictedPartItemEntity> updateList = new ArrayList<QarRestrictedPartItemEntity>();
		if (!CollectionUtils.isEmpty(list)) {
			List<QarRestrictedPartItemEntity> standerdList = copyData(list);
			for (String qarNo : qarList) {
				QueryWrapper<QarRestrictedPartEntity> queryWrapper2 = new QueryWrapper<QarRestrictedPartEntity>();
				queryWrapper2.eq("row_status", 1);
				queryWrapper2.eq("qar_no", qarNo);
				List<QarRestrictedPartEntity> selectList = qarRestrictedPartDataHandler.selectByQuery(queryWrapper2);
				for (QarRestrictedPartEntity qarRestrictedPartEntity : selectList) {
					QueryWrapper<QarRestrictedPartItemEntity> queryWrapper3 = new QueryWrapper<QarRestrictedPartItemEntity>();
					queryWrapper3.eq("row_status", 1);
					queryWrapper3.eq("parent_id", qarRestrictedPartEntity.getId());
					List<QarRestrictedPartItemEntity> selectByQuery = qarRestrictedPartItemDataHandler.selectByQuery(queryWrapper3);
					// 筛选出要新增的数据，放入大的新增集合中
					List<QarRestrictedPartItemEntity> addItemEntityList = standerdList.stream().filter(item -> !selectByQuery.contains(item)).collect(Collectors.toList());
					if (!CollectionUtils.isEmpty(addItemEntityList)) {
						List<QarRestrictedPartItemEntity> clonePartItemEntityList = clonePartItemEntityList(addItemEntityList);
						for (QarRestrictedPartItemEntity qarRestrictedPartItemEntity : clonePartItemEntityList) {
							baseEntityDataUtil.setBaseEntityData(qarRestrictedPartItemEntity);
							qarRestrictedPartItemEntity.setQarNo(qarRestrictedPartEntity.getQarNo());
							qarRestrictedPartItemEntity.setParentId(qarRestrictedPartEntity.getId());
						}
						addList.addAll(clonePartItemEntityList);
					}
					
					// 筛选出要删除的数据，放入大的删除集合中
					List<QarRestrictedPartItemEntity> deleteItemEntityList = selectByQuery.stream().filter(item -> !standerdList.contains(item)).collect(Collectors.toList());
					if (!CollectionUtils.isEmpty(deleteItemEntityList)) {
						List<QarRestrictedPartItemEntity> clonePartItemEntityList = clonePartItemEntityList(deleteItemEntityList);
						for (QarRestrictedPartItemEntity qarRestrictedPartItemEntity : clonePartItemEntityList) {
							baseEntityDataUtil.updateBaseEntityData(qarRestrictedPartItemEntity);
							qarRestrictedPartItemEntity.setRowStatus(1);
						}
						deleteList.addAll(clonePartItemEntityList);
					}
					
					// 筛选出要更新的数据（当周期和限寿方式不相同时为更新集合） 并放入大的更新集合中
					List<QarRestrictedPartItemEntity> updateItemEntityList = selectByQuery.stream().filter(item -> {
						if (standerdList.contains(item)) {
							QarRestrictedPartItemEntity entity = standerdList.stream().filter(a -> a.equals(item)).collect(Collectors.toList()).get(0);
							item.setBaseTime(entity.getBaseTime());
							item.setDueProcessing(entity.getDueProcessing());
							item.setLifeLimitedMethod(entity.getLifeLimitedMethod());
							item.setLifeLimitedTimeType(entity.getLifeLimitedTimeType());
							item.setLifeLimitedTimeValue(entity.getLifeLimitedTimeValue());
							item.setStorageTime(entity.getStorageTime());
							baseEntityDataUtil.updateBaseEntityData(item);
							return true;
						}
						return false;
					}).collect(Collectors.toList());
					if (!CollectionUtils.isEmpty(updateItemEntityList)) {
						List<QarRestrictedPartItemEntity> clonePartItemEntityList = clonePartItemEntityList(updateItemEntityList);
						updateList.addAll(clonePartItemEntityList);
					}
				}
				
			}// 12222
			if (addList.size() > 0) {
				qarRestrictedPartItemDataHandler.insertBatch(addList);
			}
			if (updateList.size() > 0) {
				qarRestrictedPartItemDataHandler.updateBatch(updateList);	
						}
			if (deleteList.size() >0) {
				qarRestrictedPartItemDataHandler.deleteBatch(deleteList);
			}
			
		}
	}
	/**
	 * 将数据转换成qar的限售件表中
	 * **/
	public List<QarRestrictedPartItemEntity> copyData (List<BasicStandardRestrictedPart> list) {
		if (!CollectionUtils.isEmpty(list)) {
			List<QarRestrictedPartItemEntity> collect = list.stream().map(item -> {
				QarRestrictedPartItemEntity entity = new QarRestrictedPartItemEntity();
				entity.setBaseTime(item.getInnerData().getBaseTime());
				entity.setDueProcessing(item.getInnerData().getDueProcessing());
				entity.setLifeLimitedMethod(item.getInnerData().getLifeLimitedMethod());
				entity.setLifeLimitedTimeType(item.getInnerData().getLifeLimitedTimeType());
				entity.setLifeLimitedTimeValue(item.getInnerData().getLifeLimitedTimeValue());
				entity.setStorageTime(item.getInnerData().getStorageTime());
				entity.setUuid(item.getInnerData().getUuid());
				return entity;
			}).collect(Collectors.toList());
			return collect;
		}
		return null;
	}
	/**
	 * 母本升版提交时更新对应数据,删除，新增，当更新时只有修改的数据才会新增
	 * **/ 
	public void submitBasicStandardUpdateData (List<BasicStandardRestrictedPart> list,long basicStandarId,long upBasicStandarId) {
		
		// 删除母本升版中原有的限寿数据
	QueryWrapper<BasicStandardRestrictedPartEntity> queryWrapper = new QueryWrapper<BasicStandardRestrictedPartEntity>();
	queryWrapper.eq("row_status", 1);
	queryWrapper.in("basic_standard_id", Arrays.asList(basicStandarId,upBasicStandarId));
	List<BasicStandardRestrictedPartEntity> selectList = basicStandardRestrictedPartDao.selectList(queryWrapper);
	if (CollectionUtils.isEmpty(list)) {
		list = new ArrayList<BasicStandardRestrictedPart>();
	}
	List<BasicStandardRestrictedPartEntity> collect = list.stream().map(item -> item.getInnerData()).collect(Collectors.toList());
	for (BasicStandardRestrictedPartEntity basicStandardRestrictedPartEntity : collect) {
			baseEntityDataUtil.setBaseEntityData(basicStandardRestrictedPartEntity);
			basicStandardRestrictedPartEntity.setBasicStandardId(basicStandarId);
			if (StringUtils.isEmpty(basicStandardRestrictedPartEntity.getUuid())) {
				basicStandardRestrictedPartEntity.setUuid(UUID.randomUUID().toString());
		}
	}
//	List<BasicStandardRestrictedPartEntity> entityList = list.stream().map(BasicStandardRestrictedPart::getInnerData).filter(item -> item != null).collect(Collectors.toList());
//	// 新增数据的集合
//	List<BasicStandardRestrictedPartEntity> addList = entityList.stream().filter(item -> {
//		item.setBasicStandardId(basicStandarId);
//		baseEntityDataUtil.setBaseEntityData(item);
//		return !selectList.contains(item);
//	}).collect(Collectors.toList());
//	// 删除数据的集合
//	List<BasicStandardRestrictedPartEntity> deleteList = selectList.stream().filter(a -> {
//		a.setBasicStandardId(basicStandarId);
//		baseEntityDataUtil.updateBaseEntityData(a);
//		return !entityList.contains(a);
//	}).collect(Collectors.toList());
//	// 更新的集合
//	// 同时将对应的更改
//	@SuppressWarnings("unlikely-arg-type")
//	List<BasicStandardRestrictedPartEntity> updateList = selectList.stream().filter(a -> {
//		if (entityList.contains(a)) {
//			BasicStandardRestrictedPartEntity entity = entityList.stream().filter(item -> item.equals(a)).collect(Collectors.toList()).get(0);
//			// 当数据中对应数据都没有更改，才能判定当前数据没有更改
//			if (entity.equals(a.getLifeLimitedTimeType()) && entity.getLifeLimitedTimeValue().equals(a.getLifeLimitedTimeValue())&&entity.getLifeLimitedMethod().equals(a.getLifeLimitedMethod())) {
//				baseEntityDataUtil.updateBaseEntityData(a);
//				a.setBaseTime(entity.getBaseTime());
//				a.setBasicStandardId(basicStandarId);
//				a.setDueProcessing(entity.getDueProcessing());
//				a.setLifeLimitedMethod(entity.getLifeLimitedMethod());
//				a.setLifeLimitedTimeType(entity.getLifeLimitedTimeType());
//				a.setLifeLimitedTimeValue(entity.getLifeLimitedTimeValue());
//				a.setStorageTime(entity.getStorageTime());
//				return true;
//			}
//		}
//		return false;
//	}).collect(Collectors.toList());
	if (collect.size() > 0) {
		this.saveBatch(collect, collect.size());
	}
	if (selectList.size() > 0) {
		selectList.forEach(item -> item.setRowStatus(0));
			this.updateBatchById(selectList, selectList.size());
		}
//	if (updateList.size() > 0) {
//		this.updateBatchById(updateList, updateList.size());
//	}
}
	// 通过母本主键查询对应限寿件数据
	public List<BasicStandardRestrictedPart> selectByBasicStandardId(long basicStandardId) {
		QueryWrapper<BasicStandardRestrictedPartEntity> queryWrapper = new QueryWrapper<BasicStandardRestrictedPartEntity>();
		queryWrapper.eq("row_Status", 1);
		queryWrapper.eq("basic_standard_id", basicStandardId);
		List<BasicStandardRestrictedPartEntity> selectList = basicStandardRestrictedPartDao.selectList(queryWrapper);
		List<BasicStandardRestrictedPart> restrictedPartList = selectList.stream().map(item -> {
			BasicStandardRestrictedPart basicStandardRestrictedPart = new BasicStandardRestrictedPart();
			basicStandardRestrictedPart.setInnerData(item);
			return basicStandardRestrictedPart;
		}).collect(Collectors.toList());
		return restrictedPartList;
	}
}
